// --------------------------------------------------------------------------------------------------------------------
// <copyright file="TitlesViewModel.cs" company="saramgsilva">
//   Copyright (c) 2012 saramgsilva. All rights reserved.
// </copyright>
// <summary>
//   The titles view model.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Netflix.PortableClassLibrary.ViewModel
{
    using System;
    using System.Collections.Generic;
    using System.Threading.Tasks;

    using GalaSoft.MvvmLight;

    using Netflix.PortableClassLibrary.Helpers;
    using Netflix.PortableClassLibrary.Model;
    using Netflix.PortableClassLibrary.Services;

    /// <summary>
    ///     The titles view model.
    /// </summary>
    public class TitlesViewModel : ViewModelBase
    {
        /// <summary>
        /// The navigation service.
        /// </summary>
        private readonly INavigationService _navigationService;

        /// <summary>
        ///     The service manager.
        /// </summary>
        private readonly IServiceManager _serviceManager;

        /// <summary>
        ///     The error message.
        /// </summary>
        private string _errorMessage;

        /// <summary>
        ///     The groups.
        /// </summary>
        private IEnumerable<GroupView> _groups;

        /// <summary>
        ///     The is to show progress.
        /// </summary>
        private bool _isToShowProgress;

        /// <summary>
        ///     Initializes a new instance of the <see cref="TitlesViewModel" /> class.
        /// </summary>
        /// <param name="navigationService">
        /// The navigation Service.
        /// </param>
        /// <param name="serviceManager">
        ///     The service Manager.
        /// </param>
        public TitlesViewModel(INavigationService navigationService, IServiceManager serviceManager)
        {
            _navigationService = navigationService;
            _isToShowProgress = true;
            _serviceManager = serviceManager;
        }

        /// <summary>
        /// Gets the error message.
        /// </summary>
        /// <value>
        /// The error message.
        /// </value>
        public string ErrorMessage
        {
            get { return _errorMessage; }
            private set { Set(ref _errorMessage, value); }
        }

        /// <summary>
        /// Gets the groups.
        /// </summary>
        /// <value>
        /// The groups.
        /// </value>
        public IEnumerable<GroupView> Groups
        {
            get { return _groups; }
            private set { Set(ref _groups, value); }
        }
        
        /// <summary>
        /// Gets a value indicating whether this instance is to show progress.
        /// </summary>
        /// <value>
        /// <c>true</c> if this instance is to show progress; otherwise, <c>false</c>.
        /// </value>
        public bool IsToShowProgress
        {
            get { return _isToShowProgress; }
            private set { Set(ref _isToShowProgress, value); }
        }
        
        /// <summary>
        ///     The load data async.
        /// </summary>
        /// <returns>
        ///     The <see cref="Task" />.
        /// </returns>
        public async Task LoadDataAsync()
        {
            IsToShowProgress = true;

            try
            {
                var newDVDMovies = _serviceManager.LoadNewDvdMoviesAsync();
                var topDVDMovies = _serviceManager.LoadTopDvdMoviesAsync();

                await TaskEx.WhenAll(new[] { newDVDMovies, topDVDMovies });

                if (newDVDMovies.IsCompleted && topDVDMovies.IsCompleted)
                {
                    Groups = new List<GroupView>
                        {
                            ViewModelHelper.GetGroupView(NameDefinitions.TopDVDMoviesId, NameDefinitions.TopDVDMoviesLabel, topDVDMovies.Result),
                            ViewModelHelper.GetGroupView(NameDefinitions.NewDVDMoviesId, NameDefinitions.NewDVDMoviesLabel, newDVDMovies.Result)
                        };
                }
                else
                {
                    ErrorMessage = GetErrorMessage(newDVDMovies, topDVDMovies);
                }

                IsToShowProgress = false;
            }
            catch (Exception exception)
            {
                IsToShowProgress = false;

                // TODO: Define a better message error for the user
                ErrorMessage = exception.Message;
            }
        }

        /// <summary>
        /// The show title details.
        /// </summary>
        /// <param name="myTitle">
        /// The my title.
        /// </param>
        /// <returns>
        /// The <see cref="bool"/>.
        /// </returns>
        public bool ShowTitleDetails(MyTitle myTitle)
        {
            if (_navigationService != null)
            {
                _navigationService.Navigate<TitleDetailsViewModel>(myTitle);
                return true;
            }

            return false;
        }

        /// <summary>
        ///     The get error message.
        /// </summary>
        /// <param name="newTitles">
        ///     The new titles.
        /// </param>
        /// <param name="topTitles">
        ///     The top titles.
        /// </param>
        /// <returns>
        ///     The <see cref="string" />.
        /// </returns>
        private string GetErrorMessage(Task<IEnumerable<MyTitle>> newTitles, Task<IEnumerable<MyTitle>> topTitles)
        {
            // TODO for each case should be defined an error message. Is not a good pratice to show a generic message that i will return :)
            // newTitles.IsCanceled - topTitles.IsCanceled - newTitles.IsFaulted topTitles.IsFaulted}
            return "In this moment is not possible to show the information. Please try later or contact the support.";
        }
    }
}